// ju_sign/suspect/index.js
import { Suspect } from '../../module/suspect.js'
const Api = new Suspect()

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:'',
    activity_id: null,
    hash: '',
    share_from: '',
    item_info: null,
    pupopRules: false,
    animationSate: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.Music = wx.getBackgroundAudioManager()

    var id = options.id ? options.id : '';
    var hash = options.hash ? options.hash : '';
    var share_from = options.share_from;
    this.setData({
      activity_id: id,
      hash: hash ? hash : '',
      share_from: share_from ? share_from : ''
    });

    this.getSuspect(this.data.activity_id, this.data.hash, this.data.share_from);

    this.Music.onEnded(() => { // 音乐自然结束事件
      this._Music()
    })
    

    this.Music.onPause(() => {
      this.setData({
        animationSate: 'animation-play-state: paused'
      })
    })

    this.Music.onPlay(() => {
      this.setData({
        animationSate: 'animation-play-state: running'
      })
    })

  },

  onJump({ currentTarget }){
    // if(this.data.item_info.status == 2){
    //   wx.showModal({
    //     title: '提示',
    //     content: `活动尚未开始`,
    //     showCancel: false,
    //     confirmColor: '#bc0000',
    //     success: (res) => {
         
    //     }
    //   })
    //   return;
    // }
    // if (this.data.item_info.status == 3) {
    //   wx.showModal({
    //     title: '提示',
    //     content: `活动已经结束`,
    //     showCancel: false,
    //     confirmColor: '#bc0000',
    //     success: (res) => {
    //     }
    //   })
    //   return;
    // }

    let index = currentTarget.dataset.index
    let music = this.data.item_info.music || ''
    let activity_id = this.data.activity_id
    wx.navigateTo({
      url: index == '1' ? 
        `./content?activity_id=${activity_id}&music=${music}` 
      : 
      `./championList?activity_id=${activity_id}` ,
    })
  },


 /**
   * 获取首页数据
   */

  getSuspect: function (id, hash, share_from){
    var that = this;
    
    Api.index({id, hash , share_from}).then((res) => {
      
      if (res.status != 1) {
        wx.showModal({
          title: '提示',
          content: res.message,
          showCancel: false,
          confirmText: '确定',
          confirmColor: '#bc0000',
          success: (res) => {
            if (res.confirm) {
              wx.navigateBack()
            }
          }
        })
        return 
      }

      wx.setNavigationBarTitle({
        title: res.data.title
      });
    
      that.setData({
        item_info: res.data,
        title: res.data.title,
      });

      this._Music()
    })
  },

  _Music() {
    let item_info = this.data.item_info
    this.Music.title = item_info.title
    this.Music.src = item_info.music
    this.Music.play()
  },

  onRules(){
    this.setData({
      pupopRules: true
    })
  },

  pupopRewardClose(){
    this.setData({
      pupopRules: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () { 
    this.Music.stop()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    var that = this;
    let title = that.data.item_info.share_title;
    return {
      title: title
    }
  },
})
